#ifndef SEQIOT_SQLITE_H
#define SEQIOT_SQLITE_H

#include <sqlite3.h>
#include "model.h"

#define SQL_DISABLE "(disable IS NULL OR disable <> 1)"

void sqlite_post_check(char *error, sqlite3 *db);

void sqlite_check(sqlite3 *db);

sqlite3 *sqlite_open(const char *filename);

sqlite3 *sqlite_open_read(const char *filename);

void read_row_str(sqlite3_stmt *stmt, int iCol, char *to, size_t size);

//以下函数为读单行数据
void read_row_object(sqlite3_stmt *stmt, object_t *rs);

void read_row_catalog(sqlite3_stmt *stmt, data_catalog_t *rs);

void read_row_thing_model(sqlite3_stmt *stmt, thing_model_t *rs);

void read_row_product(sqlite3_stmt *stmt, product_t *rs);

void read_row_device(sqlite3_stmt *stmt, device_t *rs);

void read_row_channel(sqlite3_stmt *stmt, channel_t *rs);

void read_row_uplink(sqlite3_stmt *stmt, uplink_t *rs);

//以下函数读取整张表的数据，第三个以后的参数是过滤条件
int read_stmt_catalogs(sqlite3 *db, vector_t *catalogs);

int read_stmt_products(sqlite3 *db, vector_t *products);

int read_stmt_devices(sqlite3 *db, vector_t *devices, obj_id channel);

int read_stmt_all_devices(sqlite3 *db, vector_t *devices);

int read_stmt_channel(sqlite3 *db, vector_t *channels, obj_id id);

int read_stmt_channels(sqlite3 *db, vector_t *channels);

int read_stmt_uplink(sqlite3 *db, vector_t *uplinks, obj_id id);

int read_stmt_uplinks(sqlite3 *db, vector_t *uplinks);

int read_stmt_thing_models(sqlite3 *db, product_t *product);

//获取默认配置文件名
int get_conf_file(char *filename, size_t size);

//从指定的配置文件读取配置信息
int read_config(seqiot_t *seqiot, const char *filename, obj_id channel_id, obj_id uplink_id);

#endif //SEQIOT_SQLITE_H
